<?php

/**
 * 支付宝移动端通知
 */

namespace app\member\api;

use Payment\Notify\PayNotifyInterface;

class AlipayWebApi implements PayNotifyInterface {

    /**
     * 异步回调
     */
    public function index() {
        echo target('member/AlipayWeb', 'pay')->notifyPay($this);
    }

    /**
     * 接口回调
     * @param array $data
     * @return bool
     */
    public function notifyProcess(array $data) {
        if ($data['trade_state'] <> 'success') {
            error_log('支付状态失败');
            return false;
        }
        $orderNo = $data['order_no'];
        if (empty($orderNo)) {
            error_log('支付号错误');
            return false;
        }
        $model = target('member/MemberRecharge');

        $app = $data['extra_param'];

        $model->beginTransaction();
        if(!target($app . '/PayOrder', 'service')->pay($orderNo, $data['amount'], '支付宝电脑版', $data['transaction_id'])) {
            $model->rollBack();
            error_log(target($app . '/PayOrder', 'service')->getError());
            return false;
        }
        $model->commit();
        return true;
    }

}